package com.mianyang.zhangsan.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mianyang.zhangsan.common.base.BaseController;
import com.mianyang.zhangsan.common.base.R;
import com.mianyang.zhangsan.model.entity.SecAuthority;
import com.mianyang.zhangsan.service.ISecAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理
 * @author jiangjr
 * @since 2022-10-23
 */
@RestController
@RequestMapping("/auth")
public class SecAuthorityController extends BaseController {
    @Autowired
    private ISecAuthorityService authorityService;

    /**
     * 查询所有权限
     */
    @GetMapping("/all")
    public R searchAllAuthorities(@RequestParam(value = "authorityName", required = false) String authorityName,
                                  @RequestParam(value = "current", defaultValue = "1") Integer current,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SecAuthority> authorities = authorityService.selectAuthorities(authorityName, current, pageSize);
        return success("authorities", authorities);
    }

    @GetMapping("/no/cond")
    public R queryAuthorities() {
        List<SecAuthority> authorities = authorityService.list();
        return success("authorities", authorities);
    }

    @GetMapping("/parent")
    public R queryParentAuthority() {
        List<SecAuthority> authorities = authorityService.selectParentAuthorities();
        return success("authorities", authorities);
    }


    /**
     * 新增权限
     */
    @PostMapping("/save")
    public R saveAuthority(@RequestBody SecAuthority authority) {
        authorityService.save(authority);
        return success();
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/delete/{authorityId}")
    public R deleteAuthority(@PathVariable("authorityId") Long authorityId) {
        authorityService.removeById(authorityId);
        return success();
    }

    /**
     * 修改权限
     */
    @PutMapping("/edit")
    public R editAuthority(@RequestBody SecAuthority authority) {
        authorityService.updateById(authority);
        return success();
    }
}
